package cs3343.project.family.UI;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import cs3343.project.family.CardController;
import cs3343.project.family.GameController;

/**
 * The Class CreateCardFrame.
 */
public class CreateCardFrame extends JFrame implements ActionListener{

	/** The gc. */
	private GameController gc;
	
	/** The content pane. */
	private JPanel contentPane;
	
	/** The btn return. */
	private JButton btnReturn;
	
	/** The txt zh. */
	private JTextField txtZH;
	
	/** The txt en. */
	private JTextField txtEN;
	
	/** The txt cn. */
	private JTextField txtCN;

	/**
	 * Create the frame.
	 *
	 * @param gc the gc
	 */
	public CreateCardFrame(GameController gc) {
		this.gc = gc;

		setTitle("Create Card");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0};
		gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
		
		JLabel lblCardDescription = new JLabel("Card Description:");
		GridBagConstraints gbc_lblCardDescription = new GridBagConstraints();
		gbc_lblCardDescription.insets = new Insets(0, 0, 5, 5);
		gbc_lblCardDescription.gridx = 1;
		gbc_lblCardDescription.gridy = 1;
		contentPane.add(lblCardDescription, gbc_lblCardDescription);
		
		JLabel lblZH = new JLabel("繁體中文：");
		GridBagConstraints gbc_lblZH = new GridBagConstraints();
		gbc_lblZH.insets = new Insets(0, 0, 5, 5);
		gbc_lblZH.gridx = 1;
		gbc_lblZH.gridy = 2;
		contentPane.add(lblZH, gbc_lblZH);
		
		txtZH = new JTextField();
		GridBagConstraints gbc_txtZH = new GridBagConstraints();
		gbc_txtZH.insets = new Insets(0, 0, 5, 0);
		gbc_txtZH.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtZH.gridx = 3;
		gbc_txtZH.gridy = 2;
		contentPane.add(txtZH, gbc_txtZH);
		txtZH.setColumns(10);
		
		JLabel lblEN = new JLabel("English:");
		GridBagConstraints gbc_lblEN = new GridBagConstraints();
		gbc_lblEN.insets = new Insets(0, 0, 5, 5);
		gbc_lblEN.gridx = 1;
		gbc_lblEN.gridy = 3;
		contentPane.add(lblEN, gbc_lblEN);
		
		txtEN = new JTextField();
		GridBagConstraints gbc_txtEN = new GridBagConstraints();
		gbc_txtEN.insets = new Insets(0, 0, 5, 0);
		gbc_txtEN.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtEN.gridx = 3;
		gbc_txtEN.gridy = 3;
		contentPane.add(txtEN, gbc_txtEN);
		txtEN.setColumns(10);
		
		JLabel lblCN = new JLabel("简体中文：");
		GridBagConstraints gbc_lblCN = new GridBagConstraints();
		gbc_lblCN.insets = new Insets(0, 0, 5, 5);
		gbc_lblCN.gridx = 1;
		gbc_lblCN.gridy = 4;
		contentPane.add(lblCN, gbc_lblCN);
		
		txtCN = new JTextField();
		GridBagConstraints gbc_txtCN = new GridBagConstraints();
		gbc_txtCN.insets = new Insets(0, 0, 5, 0);
		gbc_txtCN.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtCN.gridx = 3;
		gbc_txtCN.gridy = 4;
		contentPane.add(txtCN, gbc_txtCN);
		txtCN.setColumns(10);
		
		JButton btnOral = new JButton("Create Oral Card");
		btnOral.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (txtZH.getText().isEmpty() || txtCN.getText().isEmpty() || txtEN.getText().isEmpty())
					JOptionPane.showMessageDialog(null,
				            "The text box can not be empty.",
				            "Create Failed",
				            JOptionPane.ERROR_MESSAGE);
				else{
					CardController.createCard(1, txtZH.getText(), txtEN.getText(), txtCN.getText());
					JOptionPane.showMessageDialog(null, "Sucessfully created.");
				}
			}
		});
		GridBagConstraints gbc_btnOral = new GridBagConstraints();
		gbc_btnOral.insets = new Insets(0, 0, 5, 0);
		gbc_btnOral.gridx = 3;
		gbc_btnOral.gridy = 7;
		contentPane.add(btnOral, gbc_btnOral);
		
		JButton btnAction = new JButton("Create Action Card");
		btnAction.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (txtZH.getText().isEmpty() || txtCN.getText().isEmpty() || txtEN.getText().isEmpty())
					JOptionPane.showMessageDialog(null,
				            "The text box can not be empty.",
				            "Create Failed",
				            JOptionPane.ERROR_MESSAGE);
				else{
					CardController.createCard(0, txtZH.getText(), txtEN.getText(), txtCN.getText());
					JOptionPane.showMessageDialog(null, "Sucessfully created.");
				}
			}
		});
		GridBagConstraints gbc_btnAction = new GridBagConstraints();
		gbc_btnAction.gridx = 3;
		gbc_btnAction.gridy = 8;
		contentPane.add(btnAction, gbc_btnAction);
		
		btnReturn = new JButton("Return to Home");
		GridBagConstraints gbc_btnReturn = new GridBagConstraints();
		gbc_btnReturn.gridx = 3;
		gbc_btnReturn.gridy = 9;
		contentPane.add(btnReturn, gbc_btnReturn);
		
		btnReturn.addActionListener(this);
		
		gc.getUiController().frameCentering(this);
	}

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnReturn) {
			gc.getUiController().showWelcomeFrame();
			this.dispose();
		}
	}

}
